interface slideOptions {
  distance: number;
}

export const slideUp = (
  el: HTMLElement,
  callback: (...args: any) => any,
  options: slideOptions = {
    distance: 40,
  }
) => {
  let startTime: number;
  let startDistanceX: number;
  let startDistanceY: number;
  let endTime: number;
  let endDistanceX: number;
  let endDistanceY: number;
  let moveTime: number;
  let moveDistanceX: number;
  let moveDistanceY: number;

  el.addEventListener("touchstart", (e) => {
    startTime = new Date().getTime();
    startDistanceX = e.touches[0].screenX;
    startDistanceY = e.touches[0].screenY;
  });

  el.addEventListener("touchend", (e) => {
    endTime = new Date().getTime();
    endDistanceX = e.changedTouches[0].screenX;
    endDistanceY = e.changedTouches[0].screenY;
    moveTime = endTime - startTime;
    moveDistanceX = startDistanceX - endDistanceX;
    moveDistanceY = startDistanceY - endDistanceY;
    console.log("----move distance x and y", moveDistanceX, moveDistanceY);

    if (moveDistanceY > options.distance && moveTime < 500) {
      callback();
    }
  });
  // if (
  //   (Math.abs(moveDistanceX) > 40 || Math.abs(moveDistanceY) > 40) &&
  //   moveTime < 500
  // ) {
  //   // 判断X轴移动的距离是否大于Y轴移动的距离
  //   if (Math.abs(moveDistanceX) > Math.abs(moveDistanceY)) {
  //     // 左右
  //     console.log(moveDistanceX > 0 ? "左" : "右");
  //   } else {
  //     // 上下
  //     console.log(moveDistanceY > 0 ? "上" : "下");
  //   }
  // }
};

export const slideDown = (
  el: HTMLElement,
  callback: (...args: any) => any,
  options: slideOptions = {
    distance: -40,
  }
) => {
  let startTime: number;
  let startDistanceX: number;
  let startDistanceY: number;
  let endTime: number;
  let endDistanceX: number;
  let endDistanceY: number;
  let moveTime: number;
  let moveDistanceX: number;
  let moveDistanceY: number;

  el.addEventListener("touchstart", (e) => {
    startTime = new Date().getTime();
    startDistanceX = e.touches[0].screenX;
    startDistanceY = e.touches[0].screenY;
  });

  el.addEventListener("touchend", (e) => {
    endTime = new Date().getTime();
    endDistanceX = e.changedTouches[0].screenX;
    endDistanceY = e.changedTouches[0].screenY;
    moveTime = endTime - startTime;
    moveDistanceX = startDistanceX - endDistanceX;
    moveDistanceY = startDistanceY - endDistanceY;
    console.log(
      "----move distance x and y",
      moveDistanceX,
      moveDistanceY,
      options.distance
    );

    if (moveDistanceY < options.distance && moveTime < 500) {
      console.log("move down----");
      callback();
    }
  });
  // if (
  //   (Math.abs(moveDistanceX) > 40 || Math.abs(moveDistanceY) > 40) &&
  //   moveTime < 500
  // ) {
  //   // 判断X轴移动的距离是否大于Y轴移动的距离
  //   if (Math.abs(moveDistanceX) > Math.abs(moveDistanceY)) {
  //     // 左右
  //     console.log(moveDistanceX > 0 ? "左" : "右");
  //   } else {
  //     // 上下
  //     console.log(moveDistanceY > 0 ? "上" : "下");
  //   }
  // }
};
